package controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import util.CommonConstant;
import util.CommonSendRequestUtil;
import util.KKClient;
import util.KKResponse;

@Controller
public class RegisterController {
	/**
	 * pageRegister(跳转到注册页面)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 *ModelAndView
	 * @exception
	 * @since  1.0.0
	*/
	@RequestMapping ("/pageRegister")  
    public ModelAndView pageRegister(HttpServletRequest request, HttpServletResponse response) throws Exception{  
       ModelAndView modelAndView = new ModelAndView();  
       modelAndView.setViewName( "register.jsp" );  
       return modelAndView;  
    } 
	/**
	 * saveRegister(注册用户信息)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 *ModelAndView
	 * @exception
	 * @since  1.0.0
	*/
	@RequestMapping ("/saveRegister")  
    public ModelAndView saveRegister(HttpServletRequest request, HttpServletResponse response) throws Exception { 
		// 1.获取注册用户信息
		String user = request.getParameter("user");
		String postUrl = CommonConstant.baseUrl +"/user";
		JSONObject obj = JSONObject.parseObject(user);
		
		//2.发送请求
		KKResponse resp = CommonSendRequestUtil.sendRestPostWithoutTokenRequest(postUrl, obj);
		String responseBody = resp.getResponseBody();
		
		//JSONObject result = KKClient.sendPOSTRequest(postUrl,null,obj);
		PrintWriter out = response.getWriter();
		out.print(responseBody);
		out.flush();
		out.close();
		return null;
    }
}

